package com.tensquare.base.service;

import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.pojo.Label;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

/**
 * Label Service.
 *
 * @author 宫新程
 * @since 2019/3/2 11:18
 */
@Service
@Transactional
public class LabelService {

    @Resource
    private LabelDao labelDao;
    @Resource
    private IdWorker idWorker;

    public List<Label> findAll() {
        return labelDao.findAll();
    }

    public Label findById(String labelId) {
        return labelDao.findById(labelId).get();
    }

    public void save(Label label) {
        label.setId(idWorker.nextId() + "");
        labelDao.save(label);
    }

    public void delete(String labelId) {
        labelDao.deleteById(labelId);
    }

    public void update(Label label) {
        labelDao.save(label);
    }

    /**
     * 条件查询.
     */
    public List<Label> findSearch(Label label) {
        return labelDao.findAll(
                new Specification<Label>() {
                    /*  @param root 根对象, 把条件封装到哪个对象中 where 类名 = label.getId.
                     * @param query 封装的都是查询的关键字 比如：group by, order by
                     * @param cb 用来封装条件对象, 如果直接返回null 则不需要任何条件
                     * @return 列表 */
                    @Override
                    public Predicate toPredicate(
                            Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                        List<Predicate> list = new ArrayList<>();
                        if (label.getLabelname() != null && !"".equals(label.getLabelname())) {
                            Predicate predicate =
                                    cb.like(
                                            root.get("labelname").as(String.class),
                                            "%" + label.getLabelname() + "%"); // where labelname like "%admin%"
                            list.add(predicate);
                        }

                        if (label.getState() != null && !"".equals(label.getState())) {
                            Predicate predicate =
                                    cb.equal(root.get("state").as(String.class), label.getState()); // state = "1"
                            list.add(predicate);
                        }

                        Predicate[] arr = new Predicate[list.size()];
                        return cb.and(list.toArray(arr)); // where labelname like "%admin%" and state = "1"
                    }
                });
    }

    /**
     * 分页条件查询.
     */
    public Page<Label> pageQuery(Label label, int page, int size) {
        if (size == 0) {
            size = 10;
        }
        if (page <= 0) {
            page = 1;
        }
        Pageable pageable = PageRequest.of(page - 1, size);
        return labelDao.findAll(
                new Specification<Label>() {
                    /*  @param root 根对象, 把条件封装到哪个对象中 where 类名 = label.getId.
                     * @param query 封装的都是查询的关键字 比如：group by, order by
                     * @param cb 用来封装条件对象, 如果直接返回null 则不需要任何条件
                     * @return 列表 */
                    @Override
                    public Predicate toPredicate(
                            Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                        List<Predicate> list = new ArrayList<>();
                        if (label.getLabelname() != null && !"".equals(label.getLabelname())) {
                            Predicate predicate =
                                    cb.like(
                                            root.get("labelname").as(String.class),
                                            "%" + label.getLabelname() + "%"); // where labelname like "%admin%"
                            list.add(predicate);
                        }

                        if (label.getState() != null && !"".equals(label.getState())) {
                            Predicate predicate =
                                    cb.equal(root.get("state").as(String.class), label.getState()); // state = "1"
                            list.add(predicate);
                        }

                        Predicate[] arr = new Predicate[list.size()];
                        return cb.and(list.toArray(arr)); // where labelname like "%admin%" and state = "1"
                    }
                },
                pageable);
    }
}
